<?php

namespace App\Http\Controllers\Home;

use App\Model\Checkgroup;
use App\Model\Comcheckself;
use App\Model\Comcheckselfstep;
use App\Model\Commanager;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\CompanyRequest;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Auth;
use App\Model\Comhealthofficer;
use App\Model\Comsafeofficer;
use App\Model\Comspecialequipmentofficer;
use App\Model\Comspecialworkofficer;
use App\Model\Company;
use App\Model\Comcheck;
use App\Model\Comnature;
use App\Model\Comcheckstep;
use App\Model\Area;
use phpDocumentor\Reflection\Project;
use Carbon\Carbon;
use App\Model\Comcheckdetail;
use App\Model\Comcheckagain;
use App\Model\Comcheckagaindetail;
use App\Model\Checkcategory;
use Illuminate\Support\Facades\DB;

class NinesmallselfController extends Controller
{

    //消防非消防
    public function firetype(Request $request,int $companyId)
    {
        $company = Company::find($companyId);
        $request->session()->put('categoryId',$company->categoryId);

        if(!$company) {
            \Session::flash('error', '公司信息有误!');
            return back();
        }
        if(!$companyId){
            \Session::flash('error', '公司信息有误!');
            return back();
        }

        $fire = 0;
        $nofire = 0;
        $comcheckstep = new Comcheckselfstep();
        $fireinfo = $comcheckstep->getfiretype($companyId);
        if(count($fireinfo)){
            foreach ($fireinfo as $k => $v) {
                if($v->typeStatus == 1) {
                    $fire += $v->stepStatus;
                }
                if($v->typeStatus == 2) {
                    $nofire += $v->stepStatus;
                }
            }
        }
        $comcheckInfo = Comcheckself::where(['companyId'=>$companyId])->select('id','status')->first();

        if($comcheckInfo) {
            $checkId = $comcheckInfo['id'];
        } else {
            $user = Auth::user();
            $check['companyId'] = $companyId;
            $check['created'] = date('Y-m-d H:i:s',time());
            $check['userId'] = $user->id;
            $checkId = Comcheckself::insertGetId($check);
        }
        $request->session()->put('companyId', $companyId);
        $request->session()->put('checkId', $checkId);
        return view('ninesmallself.firetype')
            ->with('companyId',$companyId)
            ->with('fire',$fire)
            ->with('nofire',$nofire);
    }

    //人物环管
    public function choose(Request $request,int $firetype)
    {
        $companyId = $request->session()->get('companyId',0);
        $categoryId = $request->session()->get('categoryId',0);
        if(!$companyId) {
            return redirect('/ninesmall');
        }
        $request->session()->put('firetype', $firetype);
        if(!$firetype){
            \Session::flash('error', '公司信息有误!');
            return back();
        }
        $comcheckstep = new Comcheckselfstep();
        $fireinfo = $comcheckstep->getfiretype($companyId);
        $id1 = Checkgroup::where(['categoryId'=>$categoryId,'status'=>1,'type'=>$firetype])->first();
        $id2 = Checkgroup::where(['categoryId'=>$categoryId,'status'=>2,'type'=>$firetype])->first();
        if (empty($id1->id)){
            \Session::flash('error', '检查信息未录入!');
            return back();
        }
        if (empty($id2->id)){
            \Session::flash('error', '检查信息未录入!');
            return back();
        }
        $steps = [
            '1'=>[
                'id' => Checkgroup::where(['categoryId'=>$categoryId,'status'=>1,'type'=>$firetype])->first()->id,
                'status' => 0,
                'title'=>'基础档案'
            ],
            '2'=>[
                'id' => Checkgroup::where(['categoryId'=>$categoryId,'status'=>2,'type'=>$firetype])->first()->id,
                'status' => 0,
                'title'=>'作业现场'
            ],
        ];
        if(count($fireinfo)){
            foreach ($fireinfo as $k => $v) {
                if($v->typeStatus == $firetype) {
                    $steps[$v->stepStatus]['status'] = 1 ;
                }
            }
        }
        return view('ninesmallself.choose')
            ->with('companyId',$companyId)
            ->with('steps',$steps);
    }

}
